package com.zhuoyue.web.controller;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhuoyue.biz.core.api.dao.DeviceDao;
import com.zhuoyue.biz.core.api.dao.TblDeviceActionsMapper;
import com.zhuoyue.biz.core.api.domain.DeviceDO;
import com.zhuoyue.biz.core.api.domain.TblDeviceActions;

import com.zhuoyue.biz.core.command.impl.*;
import com.zhuoyue.biz.core.command.impl.alarm.HumanBodySensorPanelControl0Faa8105;
import com.zhuoyue.biz.core.command.impl.curtains.IntelligentCurtain0Fe60210;
import com.zhuoyue.biz.core.command.impl.curtains.SilentCurtain0Fe60209;
import com.zhuoyue.biz.core.command.impl.curtains.TranslationWindowOpener0Fe60211;
import com.zhuoyue.biz.core.command.impl.intelligence.Automate;
import com.zhuoyue.biz.core.command.impl.intelligence.Scene;
import com.zhuoyue.biz.core.command.impl.lock.FingerprintLock0Fbe0202;
import com.zhuoyue.biz.core.command.impl.lock.FingerprintLock0Fbe0203;
import com.zhuoyue.biz.core.command.impl.meterage.MeterageControlBox0Fc80202;
import com.zhuoyue.biz.core.command.impl.box.StrongCurrentControlBox0Faa0202;
import com.zhuoyue.biz.core.command.impl.box.WeakCurrentControlBox0Faa0203;
import com.zhuoyue.biz.core.command.impl.panel.*;
import com.zhuoyue.common.aop.auth.CheckLogin;
import com.zhuoyue.common.aop.auth.User;
import com.zhuoyue.common.utils.JsonUtils;
import com.zhuoyue.common.utils.Time;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;


import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhuoyue.biz.core.api.service.GatewayRedisService;
import com.zhuoyue.common.constant.DeviceConstant;
import com.zhuoyue.common.dto.ControllerResult;
import com.zhuoyue.common.utils.CmdUtils;
import org.apache.commons.lang3.StringUtils;
import com.zhuoyue.web.factory.DeviceFactory;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author wjming
 * @ClassName: AppCommandController
 * @Description:老命令控制接口
 * @Date 2021/2/14 19:39
 */
@RestController
@Scope("prototype")
@RequestMapping("/api/command")
@Validated
@Slf4j
public class AppCommandController {


    @Autowired
    private GatewayRedisService gatewayRedisService;

    @Autowired
    private DeviceDao deviceDao;

    @Autowired
    private TblDeviceActionsMapper deviceActionsMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * ff数据
     */
    private static final String DATA_FF = "FF";
    /**
     * 00数据
     */
    private static final String DATA_00 = "00";
    /**
     * 10秒
     */
    private static final int TIME_10 = 10;
    /**
     * 60秒
     */
    private static final int TIME_60 = 60;

    private static final String STR = "\\";
    private static final String TURN_OFF_REQUEST = "TurnOffRequest";
    private static final String CANCEL_DELAY_REQUEST = "CancelDelayRequest";
    private static final String CONTROL_TYPE = "controlType";
    private static final String ACTIONS = "actions";
    private static final String DATA_2A = "2A";
    private static final String DATA_23 = "23";

    /**
     * 指令处理
     *
     * @param deviceType   设备类型
     * @param purpose      意图
     * @param controlParam 控制参数
     * @return
     */
    @PostMapping(value = "/cmdDeal")
    public ControllerResult cmdDeal(@NotBlank(message = "deviceType不能为空") String deviceType,
                                    @NotBlank(message = "purpose不能为空！") String purpose,
                                    @NotBlank(message = "controlParam不能为空！") String controlParam) {
        log.info("request -> [cmdDeal] -> [deviceType:{},purpose:{},controlParam:{}]", deviceType, purpose, controlParam);
        String cmd = null;
        try {

            switch (deviceType) {
                case "DuoCaiDeng_0FAA0B02":
                    DuoCaiDeng0Faa0B02 duoCaiDeng = DeviceFactory
                            .getClass(DuoCaiDeng0Faa0B02.class);

                    cmd = duoCaiDeng.getCommand(controlParam);

                    break;
                case "QiangDianKongZhiHe_0FAA0202":
                    //配置成功后要查询数据库的值，并且保存下nightLightTime
                    //先通过zigbeeMac查询id
                    Map<String, String> map = (Map) JSONObject.parse(controlParam);
                    Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));
                    String nightLightTime = actionsMap.get("motorTimeParameter");
                    if (!StringUtils.isEmpty(nightLightTime)) {
                        System.out.println("deviceMac = " + map.get("deviceMac"));
                        List<DeviceDO> deviceDos = deviceDao.selectDeviceIdByDeviceMac(map.get("deviceMac"));
                        nightLightTime = CmdUtils.intToHexTwo(Integer.parseInt(nightLightTime));
                        List<String> id = new ArrayList<>();
                        if (!deviceDos.isEmpty()) {
                            for (DeviceDO device : deviceDos) {
                                id.add(device.getId());
                            }
                        }
                        //先修改，如果不成功就插入
                        for (int i = 0; i < id.size(); i++) {
                            int count = 0;
                            count = deviceDao.updateDeviceActionMotorTimeParameter(id.get(i), nightLightTime);
                            if (count < 1) {
                                count = deviceDao.insertDeviceActionMotorTimeParameter(id.get(i), nightLightTime);
                                if (count < 1) {
                                    return ControllerResult.toParamFail("强电控制盒电机时间配置失败");
                                }
                            }
                        }
                    }
                    cmd = StrongCurrentControlBox0Faa0202.getCommand(controlParam);
                    break;
                case "RuoDianKongZhiHe_0FAA0203":
                    //配置成功后要查询数据库的值，并且保存下nightLightTime
                    //先通过zigbeeMac查询id
                    Map<String, String> map4 = (Map) JSONObject.parse(controlParam);
                    Map<String, String> actionsMap4 = (Map) JSONObject.parse(JSON.toJSONString(map4.get("actions")));
                    String nightLightTime4 = actionsMap4.get("motorTimeParameter");
                    if (!StringUtils.isEmpty(nightLightTime4)) {
                        System.out.println("deviceMac = " + map4.get("deviceMac"));
                        List<DeviceDO> deviceDos4 = deviceDao.selectDeviceIdByDeviceMac(map4.get("deviceMac"));
                        nightLightTime4 = CmdUtils.intToHexTwo(Integer.parseInt(nightLightTime4));
                        List<String> id4 = new ArrayList<>();
                        if (!deviceDos4.isEmpty()) {
                            for (DeviceDO device : deviceDos4) {
                                id4.add(device.getId());
                            }
                        }
                        //先修改，如果不成功就插入
                        for (int i = 0; i < id4.size(); i++) {
                            int count = 0;
                            count = deviceDao.updateDeviceActionMotorTimeParameter(id4.get(i), nightLightTime4);
                            if (count < 1) {
                                count = deviceDao.insertDeviceActionMotorTimeParameter(id4.get(i), nightLightTime4);
                                if (count < 1) {
                                    return ControllerResult.toParamFail("弱电控制盒电机时间配置失败");
                                }
                            }
                        }
                    }


                    cmd = WeakCurrentControlBox0Faa0203.getCommand(controlParam);

                    break;
                case "ChangJingMianBan_0FAA8A02":
                    ChangJingMianBan0Faa8A02 changJingMianBan = DeviceFactory
                            .getClass(ChangJingMianBan0Faa8A02.class);

                    cmd = changJingMianBan.getCommand(controlParam);

                    break;
                case "YeJingChangJingKongZhiQi_0FAA8A03":
                    YeJingChangJingKongZhiQi0Faa8A03 yeJingChangJingKongZhiQi = DeviceFactory
                            .getClass(YeJingChangJingKongZhiQi0Faa8A03.class);

                    cmd = yeJingChangJingKongZhiQi.getCommand(controlParam);

                    break;
                case "ZhiNengChangJingMianBan_0FAA8A04":
                    ZhiNengChangJingMianBan0Faa8A04 zhiNengChangJingMianBan0Faa8A04 = DeviceFactory
                            .getClass(ZhiNengChangJingMianBan0Faa8A04.class);

                    cmd = zhiNengChangJingMianBan0Faa8A04.getCommand(controlParam);

                    break;

                case "BaLiuXingRenTiGanYingMianBan_0FAA8105":
                    //配置成功后要查询数据库的值，并且保存下nightLightTime
                    //先通过zigbeeMac查询id
                    Map<String, String> map86 = (Map) JSONObject.parse(controlParam);
                    Map<String, String> actionsMapmap86 = (Map) JSONObject.parse(JSON.toJSONString(map86.get("actions")));
                    String nightLightTime86 = actionsMapmap86.get("nightLightTime");
                    System.out.println("deviceMac = " + map86.get("deviceMac"));
                    List<DeviceDO> deviceDos86 = deviceDao.selectDeviceIdByDeviceMac(map86.get("deviceMac"));
                    if (!DATA_FF.equals(nightLightTime86) && !DATA_00.equals(nightLightTime86)) {
                        if (Integer.parseInt(nightLightTime86) >= TIME_10 && Integer.parseInt(nightLightTime86) <= TIME_60) {
                            nightLightTime86 = CmdUtils.intToHexTwo(Integer.parseInt(nightLightTime86));
                        }
                    }
                    List<String> id86 = new ArrayList<String>();
                    if (!deviceDos86.isEmpty()) {
                        for (DeviceDO device : deviceDos86
                        ) {
                            id86.add(device.getId());
                        }
                    }
                    //先修改，如果不成功就插入
                    for (int i = 0; i < id86.size(); i++) {
                        int count = 0;
                        count = deviceDao.updateDeviceActionNightLightTime(id86.get(i), nightLightTime86);
                        if (count < 1) {
                            count = deviceDao.insertDeviceActionNightLightTime(id86.get(i), nightLightTime86);
                            if (count < 1) {
                                return ControllerResult.toParamFail("雷达时间配置失败");
                            }
                        }
                    }

                    cmd = HumanBodySensorPanelControl0Faa8105.getCommand(controlParam);
                    break;
                case "LianKongBangDingPeiZhi":
                    LianKongBangDingPeiZhi lianKongBangDingPeiZhi = DeviceFactory
                            .getClass(LianKongBangDingPeiZhi.class);

                    cmd = lianKongBangDingPeiZhi.getCommand(controlParam);

                    break;
                case "YiDongChaZuo_0FAA0511":
                case "ShengGuangBaoJingQi_0FAA0902":
                case "TuiLaKaiChuangQi_0FAA0211":
                case "PingTuiKaiChuangQi_0FAA0212":
                case "TouYingJia_0FAA0210":
                case "JiXieShouKongZhiQi_0FAA0213":
                case "FaMenKongZhiQi_0FAA0213":
                case "YiLuMianBan_0FAA0502":
                case "YiLuLianKongMianBan_0FAA0504":
                case "ErLuMianBan_0FAA0602":
                case "ErLuLianKongMianBan_0FAA0604":
                case "SanLuMianBan_0FAA0702":
                case "SanLuLianKongMianBan_0FAA0704":
                case "SiLuLianKongMianBan_0FAA0804":
                    //四路面板cmd
                    cmd = FourWayJointControlPanel0Faa0804.getCommand(controlParam);
                    break;

                case "LiuLuChangJing_0FAA8A05":
                    LiuLuChangjing0Faa8A05 liuLuChangjing0Faa8A05 = DeviceFactory.getClass(LiuLuChangjing0Faa8A05.class);

                    //配置成功后要查询数据库的值，并且保存下nightLightTime
                    //先通过zigbeeMac查询id
                    Map<String, String> map3 = (Map) JSONObject.parse(controlParam);
                    Map<String, String> actionsMap3 = (Map) JSONObject.parse(JSON.toJSONString(map3.get("actions")));
                    String nightLightTime3 = actionsMap3.get("nightLightTime");
                    System.out.println("deviceMac = " + map3.get("deviceMac"));
                    List<DeviceDO> deviceDos3 = deviceDao.selectDeviceIdByDeviceMac(map3.get("deviceMac"));
                    if (!DATA_FF.equals(nightLightTime3) && !DATA_00.equals(nightLightTime3)) {
                        if (Integer.parseInt(nightLightTime3) >= TIME_10 && Integer.parseInt(nightLightTime3) <= TIME_60) {
                            nightLightTime3 = CmdUtils.intToHexTwo(Integer.parseInt(nightLightTime3));
                        }
                    }
                    List<String> id3 = new ArrayList<String>();
                    if (!deviceDos3.isEmpty()) {
                        for (DeviceDO device : deviceDos3
                        ) {
                            id3.add(device.getId());
                        }
                    }
                    //先修改，如果不成功就插入
                    for (int i = 0; i < id3.size(); i++) {
                        int count = 0;
                        count = deviceDao.updateDeviceActionNightLightTime(id3.get(i), nightLightTime3);
                        if (count < 1) {
                            count = deviceDao.insertDeviceActionNightLightTime(id3.get(i), nightLightTime3);
                            if (count < 1) {
                                return ControllerResult.toParamFail("雷达时间配置失败");
                            }
                        }
                    }

                    cmd = liuLuChangjing0Faa8A05.getCommand(controlParam);
                    break;

                //八路控制器
                case "BaLuKongZhiQi_0FAB0202":
                    cmd = EightWayController0Fab0202.getCommand(controlParam);
                    break;
                //四路联控面板
                case "SiLuLianKongMianBan_0FAB0602":
                    Map<String, String> map1 = (Map) JSONObject.parse(controlParam);
                    Map<String, String> actionsMap1 = (Map) JSONObject.parse(JSON.toJSONString(map1.get("actions")));
                    String nightLightTime1 = actionsMap1.get("nightLightTime");
                    if (!DATA_FF.equals(nightLightTime1) && !DATA_00.equals(nightLightTime1)) {
                        if (Integer.parseInt(nightLightTime1) >= TIME_10 && Integer.parseInt(nightLightTime1) <= TIME_60) {
                            nightLightTime1 = CmdUtils.intToHexTwo(Integer.parseInt(nightLightTime1));
                        }
                    }
                    //配置成功后要查询数据库的值，并且保存下nightLightTime
                    //先通过zigbeeMac查询id
                    if (!StringUtils.isEmpty(nightLightTime1)) {
                        List<DeviceDO> deviceDos = deviceDao.selectDeviceIdByDeviceMac(map1.get("deviceMac"));
                        List<String> id1 = new ArrayList<String>();
                        if (!deviceDos.isEmpty()) {
                            for (DeviceDO device : deviceDos
                            ) {
                                id1.add(device.getId());
                            }
                        }
                        //先修改，如果不成功就插入
                        for (int i = 0; i < id1.size(); i++) {
                            int count = 0;
                            count = deviceDao.updateDeviceActionNightLightTime(id1.get(i), nightLightTime1);
                            if (count < 1) {
                                count = deviceDao.insertDeviceActionNightLightTime(id1.get(i), nightLightTime1);
                                if (count < 1) {
                                    return null;
                                }
                            }
                        }
                    }
                    cmd = FourWayJointControlPanel0Fab0602.getCommand(controlParam);
                    break;
                case "ZhiWenSuo_CongMingWu_0FBE0202":
                    cmd = FingerprintLock0Fbe0202.getCommand(controlParam);
                    break;
                case "ZhiWenSuo_LiuFu_0FBE0203":
                    cmd = FingerprintLock0Fbe0203.getCommand(controlParam);
                    break;
                case "JiLiangChaZuo":
                case "JiLiangChaZuo_16A_0FC80403":
                case "JiLiangChaZuo_10A_0FC80402":
                case "JiLiangKongZhiHe_0FC80202":
                    cmd = MeterageControlBox0Fc80202.getCommand(controlParam);
                    break;

                case "JingYinChuangLian_0FE60209":
                    cmd = SilentCurtain0Fe60209.getCommand(controlParam);

                    break;
                case "PingYiKaiChuangQi_0FE60211":
                    cmd = TranslationWindowOpener0Fe60211.getCommand(controlParam);
                    break;
                case "ZhiNengChuangLian_0FE60210":
                    cmd = IntelligentCurtain0Fe60210.getCommand(controlParam);
                    break;
                case "ShuangSeWen_0FAA0D02":
                    if (controlParam.indexOf(STR) != -1) {
                        controlParam = StringUtils.replace(controlParam, STR, "");
                    }
                    Map<String, Object> sswMap = JsonUtils.jsonToMap(controlParam);
                    String gatewayMac = (String) sswMap.get("gatewayMac");
                    String deviceMac = (String) sswMap.get("deviceMac");
                    DeviceDO deviceDO = deviceDao.selectByMac(deviceMac);
                    List<TblDeviceActions> deviceActions = deviceActionsMapper.selectList(Wrappers.<TblDeviceActions>lambdaQuery()
                            .eq(TblDeviceActions::getDeviceId, deviceDO.getId()));

                    cmd = ShuangSeWen0Faa0D02.getCommand(sswMap, gatewayMac, deviceMac, deviceActions, false);

                    try {
                        if (TURN_OFF_REQUEST.equals(sswMap.get(CONTROL_TYPE)) || CANCEL_DELAY_REQUEST.equals(sswMap.get(CONTROL_TYPE))) {
                            //actions为空时toString会报错
                            Map<String, Object> actions = new HashMap<>(5);
                            if (sswMap.get(ACTIONS) != null) {
                                actions = JsonUtils.jsonToMap(sswMap.get("actions").toString());
                            }
                            Integer delayTime = (Integer) actions.get("delayTime");
                            if (actions == null || delayTime == null || delayTime == 0) {
                                Boolean delete = redisTemplate.delete(deviceMac + "_delayTime");
                                if (delete) {
                                    //发送取消命令
                                    String rCmd = ShuangSeWen0Faa0D02.setCancelDelay(gatewayMac, deviceMac, "0000", "0000", "0000", "0000");
                                    gatewayRedisService.writeDataToGateway(gatewayMac, rCmd);
                                    Thread.sleep(200);
                                    System.out.printf("延时取消成功");
                                }
                            } else {
                                Long aLong = Time.toLongMilli(LocalDateTime.now()) + delayTime * 1000;
                                Map<String, Object> dTime = new HashMap<>(2);
                                dTime.put("delayTime", aLong);
                                dTime.put("type", "group");
                                redisTemplate.opsForValue().set(deviceMac + "_delayTime", aLong, delayTime, TimeUnit.SECONDS);

                            }
                        } else {
                            Map<String, Object> actions = JsonUtils.jsonToMap(sswMap.get("actions").toString());
                            Integer delayTime = (Integer) actions.get("delayTime");
                            if (delayTime != null && delayTime > 0) {
                                Long aLong = Time.toLongMilli(LocalDateTime.now()) + delayTime * 1000;
                                redisTemplate.opsForValue().set(deviceMac + "_delayTime", aLong, delayTime, TimeUnit.SECONDS);
                            }
                        }
                    } catch (Exception e) {
                        System.out.printf(e.getMessage());
                    }

                    break;
                case "WenShiDuChuanGanQi_0FBA002":
                    Map<String, String> mapA002 = (Map) JSONObject.parse(controlParam);
                    Map<String, String> actionsMapA002 = (Map) JSONObject.parse(JSON.toJSONString(mapA002.get("actions")));
                    break;

                case "SCENE":
                    cmd = Scene.getCommand(controlParam);

                    break;
                //自动化场景
                case "AUTOMATE":
                    Automate automate = DeviceFactory.getClass(Automate.class);

                    cmd = automate.getCommand(controlParam);

                    break;
                //红外设备
                case "INFRARED":
                    Infrared infrared = DeviceFactory.getClass(Infrared.class);

                    cmd = infrared.getCommand(controlParam);

                    break;
                case "GATEWAY":
                    Gateway gateway = DeviceFactory.getClass(Gateway.class);
                    cmd = gateway.getCommand(controlParam);
                    break;

                case "BeiJingYinYue_Fhwise_0FE60203":

                    BeiJingYinYueFhwise0Fe60203 beiJingYinYueFhwise0Fe60203 = DeviceFactory
                            .getClass(BeiJingYinYueFhwise0Fe60203.class);

                    cmd = beiJingYinYueFhwise0Fe60203.getCommand(controlParam);

                    break;
                default:
                    return null;
            }
            //获取控制命令
            if (DeviceConstant.RequestPurpose.GET_CMD.equals(purpose)) {
                return ControllerResult.toReturnDataSuccess("成功", cmd);
            }
            //控制设备
            else if (DeviceConstant.RequestPurpose.CONTROL_DEVICE
                    .equals(purpose)) {
                Map<String, String> map = (Map) JSONObject.parse(controlParam);
                String gatewayMac = map.get("gatewayMac");
                // 发送指令
                gatewayRedisService.writeDataToGateway(gatewayMac, cmd);
                return ControllerResult.toReturnMessageSuccess("控制命令已发送！");
            }
            log.info("result -> [cmdDeal] -> [{}]", cmd);
        } catch (Exception e) {
            log.error("cmdDeal system error -> [{}]", e.getMessage());
            return ControllerResult.toSysErrorFail("设备控制系统异常！");
        }
        return null;
    }


    /**
     * 安卓苹果自己拼接数据帧时用发送数据帧的接口
     *
     * @param gatewayMac
     * @param cmd
     * @return
     */
    @PostMapping("/cmdSend")
    public ControllerResult frontEndCommandSend(@NotBlank(message = "网关Mac不能为空") String gatewayMac,
                                                @NotBlank(message = "命令数据帧不能为空") String cmd) {
        log.info("request -> [frontEndCommandSend] -> [gatewayMac:{},cmd:{}]", gatewayMac, cmd);
        // 发送指令
        gatewayRedisService.writeDataToGateway(gatewayMac, cmd);
        return ControllerResult.toReturnDataSuccess("控制命令已发送", "{}");
    }


    /**
     * 后台对于一些一直在发数据的网关发送网关复位指令
     *
     * @param gatewayMac
     * @param cmd
     * @return
     */
    @PostMapping("/manage/cmd/send")
    @CheckLogin
    public ControllerResult manageSendCmd(@NotBlank(message = "网关Mac不能为空") String gatewayMac,
                                          @NotBlank(message = "命令数据帧不能为空") String cmd) {
        if (!StringUtils.startsWithIgnoreCase(DATA_2A, cmd) && !StringUtils.endsWithIgnoreCase(DATA_23, cmd)) {
            cmd = DATA_2A + CmdUtils.getDateLength(cmd) + cmd + CmdUtils.getCmdBodySum(cmd) + DATA_23;
        }
        log.info("request -> [frontEndCommandSend] -> [gatewayMac:{},cmd:{}]", gatewayMac, cmd);
        // 发送指令
        gatewayRedisService.writeDataToGateway(gatewayMac, cmd);
        return ControllerResult.toReturnDataSuccess("控制命令已发送", cmd);
    }


    /**
     * 清除联控绑定关系
     *
     * @param deviceType
     * @param purpose
     * @param controlParam
     * @return
     */
    @PostMapping("/cmdDealClear")
    @CheckLogin
    public ControllerResult cmdDealClear(@NotBlank(message = "设备类型不能为空") String deviceType,
                                         @NotBlank(message = "意图不能为空") String purpose,
                                         @NotBlank(message = "控制参数不能为空") String controlParam, HttpServletRequest request) {
        User user = (User) request.getAttribute("user");
        log.info("request -> user[{}_{}] -> [deviceType:{},purpose:{},controlParam:{}]", user.getAccount(), user.getOrgCode(), deviceType, purpose, controlParam);
        String cmd;
        try {
            String gatewayMac = "";
            Map<String, String> map = (Map) JSONObject.parse(controlParam);
            gatewayMac = map.get("gatewayMac");
            String sourceDevice = map.get("sourceDeviceMac");
            String cmdStr = "0201" + gatewayMac + sourceDevice + "177321" + "03" + "FFFFFFFFFFFFFFFFFFFFFFFF";
            String length = CmdUtils.getDateLength(cmdStr);
            String sum = CmdUtils.getCmdBodySum(cmdStr);
            cmd = "2A" + length + cmdStr + sum + "23";
            gatewayRedisService.writeDataToGateway(gatewayMac, cmd);
        } catch (Exception e) {
            log.error("result -> [cmdDealClear] -> [{} -> {}]", "参数 cmdDealClear处理系统异常2！", e.getMessage());
            return ControllerResult.toSysErrorFail("设备控制系统异常");
        }
        log.info("result -> [cmdDealClear] -> [{} -> {}]", "清空参数", cmd);
        return ControllerResult.toReturnMessageSuccess("控制命令已发送!");
    }
}
